/// [Lexicographic permutations](https://projecteuler.net/problem=24)
/// the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9
/// lexicographic permutation 1         : 0123456789
/// lexicographic permutation 2         : 0123456798
/// lexicographic permutation 5         : 0123456978
/// lexicographic permutation 6         : 0123456987
/// lexicographic permutation 7         : 0123457689
fn main() {
    for len in &[5, 6, 7, 1000000] {
        let mut num_vec = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        print!("{}\t", len);
        let mut len = lexicographic_permutations(&len, &mut num_vec);
        while len != 0 {
            len = lexicographic_permutations(&len, &mut num_vec);
        }
        if len == 0 {
            while let Some(item) = num_vec.pop() {
                print!("{}", item);
            }
            println!("");
        }
    }
}
fn lexicographic_permutations(length: &usize, num_vec: &mut Vec<usize>) -> usize {
    let mut length = *length;
    for n in 1..=length {
        let rst = arrangement(n);
        if rst * (n + 1) >= length {
            for _ in 0..(num_vec.len() - n - 1) {
                print!("{}", num_vec[0]);
                num_vec.remove(0);
            }
            let m = (length - 1) / rst;
            print!("{}", num_vec[m]);
            num_vec.remove(m);
            length = length % rst;
            break;
        }
    }
    length
}
fn arrangement(length: usize) -> usize {
    (1..=length).fold(1, |mul, x| mul * x)
}
